const api = require("../request/api")
const request = require("../request/request")
const util = require("../utils/util")
const App = getApp();

Page({
    data: {
        id: 1,
        uid: 9,
        page: 1,
        page_size: 15,
        shop_list: [],

        indicatorLeft: 0,
        selectedTab: 1,

        announcements: [
            "恭喜XXX成功入驻",
            "恭喜YYY成功入驻",
            "恭喜ZZZ成功入驻"
        ]
    },

    onLoad() {
        this.setIndicatorPosition(this.data.selectedTab);
        this.get_shop_list()
    },

    changeTab: function (e) {
        let tabId = parseInt(e.currentTarget.dataset.id);
        this.setData({ selectedTab: tabId });
        this.setIndicatorPosition(tabId);
    },

    setIndicatorPosition(tabId) {
        const query = wx.createSelectorQuery();
        query.select('.tabs').boundingClientRect();
        query.selectViewport().scrollOffset();

        query.exec((res) => {
            const tabsRect = res[0]; // .tabs node info
            const tabWidth = tabsRect.width / 3; // 3 tabs in total

            const indicatorWidth = 50 / 750 * tabsRect.width; // converted 50rpx to px
            const indicatorLeft = (tabId - 1) * tabWidth + (tabWidth - indicatorWidth) / 2;

            this.setData({
                indicatorLeft: indicatorLeft * 750 / wx.getSystemInfoSync().windowWidth // convert px back to rpx
            });
        });
    },

    go_search() {
        wx.navigateTo({
            url: '/pages/good/search',
        })
    },

    go_settling() {
        wx.navigateTo({
            url: '/pages/shop/settling',
        })
    },

    get_shop_list() {
        let params = {
            page: this.data.page,
            pageSize: 6,
            uid: this.data.uid
        }
        request.post(api.shoplist, params).then(res => {
            this.setData({
                shop_list: res
            })
        })
    },

    go_shop_detail(e) {
        let id = e.currentTarget.dataset.id
        if (!id) {
            id = 32
        }
        wx.navigateTo({
            url: '/pages/shop/detail?id=' + id,
        })
    },

    go_restaurant(e) {
        console.log(e);
        let index = e.currentTarget.dataset.index
        console.log(index)
        wx.navigateTo({
            url: '/pages/shop/shop?index=' + index,
        })
    }

})